package com.jiaquyun.jcyx.jetpack.api

import com.blankj.utilcode.util.GsonUtils
import com.jiaquyun.jcyx.app.JCApplication
import com.jiaquyun.jcyx.entity.*
import com.jiaquyun.jcyx.base.BaseManager
import com.module.lemlin.http.HttpResponseData
import rxhttp.RxHttp
import rxhttp.await


class GoodsRepository : BaseManager() {

    /**
     * 分类
     * */
    suspend fun goodsCategoryList(pid: String): CatResponse =
        RxHttp.get("/api/goods/categoryList")
            .add("pid", pid)
            .await()

    /**
     * 详情/api/goods/goodsInfo
     * */
    suspend fun goodsInfo(
        id: String
    ): GoodsDetailsResponse = RxHttp.get("/api/goods/goodsInfo")
        .add("id", id)
        .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
        .await()

    /**
     * 收藏/取消
     * */
    suspend fun goodsCollect(
        goodsId: String,  type: String
    ): HttpResponseData = RxHttp.get("/api/goods/collectGoods")
        .add("goods_id", goodsId)
        .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
        .add("type", type)
        .await()

    /**
     * 列表
     * */
    suspend fun goodsList(goodsRequest: GoodsRequest): GoodsListResponse {
        val maps: Map<String, String?> =
            GsonUtils.fromJson<Map<String, String?>>(
                GsonUtils.toJson(goodsRequest),
                Map::class.java
            ).filter { it.value != null }
        return RxHttp.get("/api/goods/goodsList")
            .addAll(maps)
            .await()
    }

    /**
     * 商品活动
     * */
    suspend fun goodsActivity(id: String): GoodsActiveResponse =
        RxHttp.postForm("/api/goods/goodsActivity")
            .add("id", id)
            .await()
}